
/*
树形结构 操作相关
list 转 tree 树操作
https://blog.csdn.net/weixin_44145894/article/details/128643656
树形结构的CRUD
https://www.jb51.net/article/254210.htm

*/

function addLevelToTreeData(treeData, level = 1) {
  // 递归给树形数据加 level 标识
  for (let i = 0; i < treeData.length; i++) {
    const node = treeData[i];
    node.level = level;
    if (node.children) {
      addLevelToTreeData(node.children, level + 1);
    }
  } 
  return treeData;
}

function arrTotree(list, rootValue) {
  // 递归 list 数据转 tree 数据
	var arr = []
	list.forEach(item => {
		if (item.parent === rootValue) {
			const children = arrTotree(list, item.id)
			if (children.length) {
				item.children = children
			}
			arr.push(item)
		}
	})
	return arr
}

export default {
  addLevelToTreeData,
  arrTotree,
};
